from common.k_requests import yu_requestsC
from common.b_yamlOperate import *


envData = YamlRead(pathConf.get_userFilePath("data/a_envData/env.yaml"))
class Req_api():
    """请求接口"""

    def __init__(self):
        """
        :param domain : 域名,默认测试环境
        :author 露驿星程
        """
        self.request = yu_requestsC
        self.dic = {}                     # 存储运行时提取的数据

    def choise_domain(self,uri):
        """
        选择域名
        :param uri : 传入uri
        :author 露驿星程
        """
        run_type = envData.getdata()['run_type']
        if run_type == 0:
            protocol = "http://"
            return protocol + envData.getdata()['test_domain']
        elif run_type == 1:
            protocol = "https://"
            if uri in envData.getdata()['mall_domain_uri']:
                return protocol + envData.getdata()['prod_mall_domain']
            elif uri in envData.getdata()['live_domain_uri']:
                return protocol + envData.getdata()['prod_live_domain']

    def newGoodsSpuDetail(self,spuId=None,userId=None):
        """
        商品详情接口
        :param domain : 域名,默认测试环境
        :author 露驿星程
        """
        if spuId == None:
            spuId = 189
        if userId == None:
            userId = 9475830

        uri = "/front/p/newGoodsSpu/newGoodsSpuDetail"
        domain = self.choise_domain(uri)
        method = "POST"
        url = domain + uri
        json = {}
        json['spuId'] = spuId
        json['userId'] = userId
        res = self.request.send_request(method=method, url=url, json=json, headers=userId)
        # 防止出现响应文本为乱码，方法1
        res.encoding = 'utf-8'
        # 防止出现响应文本为乱码，方法2
        # res.encoding("latin-1")
        # res.decode("utf-8")
        response = res.text
        print(response)



if __name__ == "__main__":
    pass
    # apis = Req_api()
    # apis.newGoodsSpuDetail()